﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AllInOne.Pages
{
    /// <summary>
    /// CarNumberInputPage.xaml 的交互逻辑
    /// </summary>
    public partial class CarNumberInputPage : Page
    {
        private MainWindow parent;
        public CarNumberInputPage()
        {
            InitializeComponent();
        }

        public CarNumberInputPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }



        public void InitPage()
        {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));
            this.txtPrefix.Text = "鲁";
            this.txtCarNumber.Text = "";
            this.parent.PlaySound(Properties.Resources.male_input_car_number, Properties.Resources.female_input_car_number);

        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }

        private void setBackgroundImage(object sender, string url)
        {
            TextBlock btn = (TextBlock)sender;
            btn.Background = new ImageBrush
            {
                ImageSource = new BitmapImage(new Uri(url)),
                Stretch = Stretch.Uniform
            };
        }

        private void BigBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn.png");
        }

        private void BigBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn_hover.png");
        }

        private void BigBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/big_btn.png");
            TextBlock btn = (TextBlock)sender;
            this.txtPrefix.Text = btn.Text;
        }

        private void SmallBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

        }

        private void SmallBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn_hover.png");
        }

        private void SmallBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/small_btn.png");

            TextBlock btn = (TextBlock)sender;
            string carNumber = this.txtCarNumber.Text;
            if (carNumber.Length < 6||(carNumber.Length < 7 && (carNumber.Substring(1,1)=="D"|| carNumber.Substring(1, 1) == "F")))
            {
                this.txtCarNumber.Text = carNumber + btn.Text;
            }
        }

        private void DeleteBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");

        }

        private void DeleteBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn_hover.png");
        }

        private void DeleteBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/delete_btn.png");
            if (this.txtCarNumber.Text.Length > 0)
            {
                string carNumber = this.txtCarNumber.Text;
                this.txtCarNumber.Text = carNumber.Substring(0, carNumber.Length - 1);
            }
        }

        private void ClearBtn_MouseLeave(object sender, MouseEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");

        }

        private void ClearBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn_hover.png");

        }

        private void ClearBtn_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.setBackgroundImage(sender, "pack://application:,,,/images/clear_btn.png");
            this.txtCarNumber.Text = "";
        }

        private void BtnSubmit_MouseUp(object sender, MouseButtonEventArgs e)
        {

            if (this.verifyCarNumber())
            {
                string carNumber = this.txtCarNumber.Text;
                this.parent.SetCarNumber(carNumber);
            }
            else { this.parent.Alert("提示", "您输入的号牌号码不正确，请检查您的输入。"); }



        }

        private bool verifyCarNumber()
        {

            string carNumber = this.txtCarNumber.Text;
            if (carNumber.Length >= 6)
            {
                return true;
            }


            return false;
        }
    }
}
